import base64
import time
import cv2
import numpy

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

def login_verification(user,password):
    for i in range(3):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--headless')
        chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
        driver=webdriver.Chrome(executable_path="./chromedriver.exe", chrome_options=chrome_options)
        driver.maximize_window()
        driver.get('https://icrm.800best.com/#/login')
        time.sleep(0.5)
        driver.find_element_by_xpath('//*[@id="root"]/section/div/div[2]/form/div[1]/div/div/span/div/span/span[1]').click()
        time.sleep(0.5)
        driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div/div[1]/img').click()
        time.sleep(0.5)
        use=driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div/div[2]/form/div[1]/div/div/input')
        use.click()
        use.send_keys(user)
        time.sleep(0.5)
        pas=driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div/div[2]/form/div[2]/div/div/input')
        pas.click()
        pas.send_keys(password)
        driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div/div[2]/form/button').click()
        time.sleep(1)
        img=driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/img[1]').get_property('src').split(',')[-1]
        img2=driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/img[2]').get_property('src').split(',')[-1]
        if not img or not img2:
            time.sleep(2)
            img = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/img[1]').get_property('src').split(',')[-1]
            img2 =driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/img[2]').get_property('src').split(',')[-1]
        imagedata = base64.b64decode(img)  # 解码
        imagedata2 = base64.b64decode(img2)
        with open('../resouce_img/1.jpg','wb') as f:
            f.write(imagedata)
        with open('../resouce_img/2.jpg','wb') as f:
            f.write(imagedata2)

        bg = cv2.imread('../resouce_img/1.jpg')
        bg = cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY)

        front = cv2.imread("../resouce_img/2.jpg")
        front = cv2.cvtColor(front, cv2.COLOR_BGR2GRAY)
        result = cv2.matchTemplate(bg, front, cv2.TM_CCOEFF_NORMED)
        x, y = numpy.unravel_index(numpy.argmax(result), result.shape)
        print(x,y)
        div=driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[4]/div[2]')
        ActionChains(driver).drag_and_drop_by_offset(div, xoffset=y, yoffset=0).perform()
        try:
            time.sleep(2)
            str_yan = driver.find_element_by_xpath('//*[@id="container_demo"]/div/div/div/ul/div[1]/div[1]').text
            if str_yan=="授权信息":
                BEST = "验证成功"
            else:
                BEST = "验证失败"
            driver.quit()
            break
        except Exception as e:
            BEST = "验证失败"
            driver.quit()
            continue
    return BEST


# login_verification("Fcb8080","FANCHUANGbo8080")